// packageOrder/pages/order/order-list.js
import OrderService from '../../../service/OrderService';
import WalletService from '../../../service/WalletService';
import CommonService from '../../../service/CommonService';
import { throttle, moment } from '../../../utils/util.js';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    menus: [
      { type: 1, label: '支付信用' },
      { type: 2, label: '加急订单' },
      { type: 3, label: '预算最高' },
      { type: 4, label: '红包订单' }
    ],
    result: {list: [], page: {}},
    param: { pageNum: 1, pageSize: 10, type: 1, status: 0 },
    envelope: {},
    envelopes: {},
    bonused: false,
    evnelopShow: false,
    city: {},
    user: app.globalData.user
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { productType } = options;
    const { param } = this.data;
    if (productType){
      param.productType = productType;
      this.setData({ param });
    }
    
    this.orderService = new OrderService()
    this.walletService = new WalletService()
    this.commonService = new CommonService()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { param } = this.data;
    const city = app.globalData.chooseCity;
    param.adcode = city.id;
    this.setData({ city, param })
    this._loadData(param);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.globalData.chooseCity = app.globalData.curCity;
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    const _ = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '订单分享',
      path: '/packageOrder/pages/order/order-detail',
      success: function (res) {
        console.log(res)
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
          const { shareScore } = app.globalData.setting;
          _.commonService.active(1).then(function(res){
            if(res.status == 0){
              wx.showModal({
                title: '谢谢分享',
                content: `恭喜你获得${shareScore}枚救兵勋章`,
                showCancel: false,
                confirmText: '知道了',
                confirmColor: '#0f9bc1'
              })
            }
          })
        }
      },
    }
  },
  onMenuChange: function(e){
    const { param } = this.data;
    param.type = e.detail.type;
    this._loadData(param);
    this.setData({param});
  },
  onLocationTap: throttle(function (e) {
    wx.navigateTo({
      url: '/pages/citys/citys'
    })
  }, 1000),
  onUserTap: function(e){
    throttle(function () {
      const {user} = app.globalData;
      console.log(user);
      if(user.authStatus != 2){
        wx.navigateTo({
          url: '/packageUser/pages/auth/auditting'
        })
      }else{
        wx.navigateTo({
          url: '/packageUser/pages/user/user'
        })
      }
    }, 1000)()
  },
  onShare: function(){
    const _ = this;
    wx.showShareMenu({
      withShareTicket: true,
      success: function(){
        console.log('aaaa')
      }
    })
  },
  _loadData:function(param){
    const _ = this;
    _.orderService.list(param).then(function (res) {
      if (res.status == 0) {
        res.data.list.map(item=>{
          item.createTime = moment(item.createTime).format("YYYY-MM-DD HH:mm");
          return item;
        })
        _.setData({ result: res.data })
      }
    })
  },
  _loadEnvelopeInfo:function(orderId){
    const _ = this;
    this.walletService.getEnvelopesByOrderId(orderId).then(function (res) {
      if (res.status == 0) {
        _.setData({
          evnelopShow: true,
          bonused: res.data.bonused,
          envelopes: res.data.envelopes,
          envelope: res.data.envelope
        });
      }
    });
  },
  onEvnelopBonus:function(e){
    const orderId = e.target.dataset.id;
    this.setData({ orderId: orderId});
    this._loadEnvelopeInfo(orderId);
  },
  onEnvelopClose:function(e){
    this.setData({ evnelopShow: false });
  },
  doBonus:function(e){
    const _ = this;
    const { orderId } = this.data;
    _.walletService.bonus(e.detail.envelopesId).then(function (result) {
      if(result.status == 0){
        _._loadEnvelopeInfo(orderId);
      }
    })
  },
  onItemTap: function(e){
    const { id } = e.target.dataset;
    wx.navigateTo({
      url: '/packageOrder/pages/order/order-detail?id=' + id
    })
  },
  toRuZhu: function(e){
    wx.showActionSheet({
      itemList: ['工厂方入驻', '订单方入驻'],
      success: function (res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0){
          wx.navigateTo({
            url: '/packageUser/pages/auth/auth-rules'
          })
        }else{
          wx.navigateTo({
            url: '/packageUser/pages/auth/business-auth'
          })
        }
        
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  }
})